<?php

//add_filter('show_admin_bar', '__return_false');

// Register Nav Menu
function azio_register_nav_menu()
{
    register_nav_menu('main-menu', __('Main Menu'));
    register_nav_menu('footer-menu', __('Footer Menu'));
}

add_action('init', 'azio_register_nav_menu');


// change url theme option
function change_url_theme_option(&$item, $key, $array_url)
{
    $item = str_replace($array_url['old'], $array_url['new'], $item);
}
function azio_change_theme_option($old_address, $new_address)
{
    $array_url['old'] = $old_address;
    $array_url['new'] = $new_address;
    $array_option = get_option('option_tree');
    array_walk_recursive($array_option, 'change_url_theme_option', $array_url);
    echo '<textarea rows="10" cols="100">' . serialize($array_option) . '</textarea>';
}

// Class Menu Walker
class add_span_walker extends Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
    {
        global $wp_query;
        $indent = ($depth) ? str_repeat("\t", $depth) : '';

        $class_names = '';

        $classes = empty($item->classes) ? array() : (array)$item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
        $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';

        $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
        $id = $id ? ' id="' . esc_attr($id) . '"' : '';

        $output .= $indent . '<li' . $id . $class_names . '>';

        $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';

        $item_output = $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;

        if ('primary' == $args->theme_location) {
            $submenus = 0 == $depth || 1 == $depth ? get_posts(array('post_type' => 'nav_menu_item', 'numberposts' => 1, 'meta_query' => array(array('key' => '_menu_item_menu_item_parent', 'value' => $item->ID, 'fields' => 'ids')))) : false;
            $item_output .= !empty($submenus) ? (0 == $depth ? '<span class="arrow"></span>' : '<span class="sub-arrow"></span>') : '';
        }
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    }
}


add_filter('mce_buttons', 'add_nextpage_button');

function add_nextpage_button($buttons)
{
    //array_splice so we can insert the new item without overwriting an existing button
    array_splice($buttons, 15, 0, 'wp_page');
    return $buttons;
}

add_action('admin_menu', 'my_remove_menu_pages', 9999);

function my_remove_menu_pages()
{
    remove_menu_page('link-manager.php');
    remove_menu_page('edit-comments.php');

    remove_submenu_page( 'themes.php', 'theme-editor.php' );
    remove_submenu_page( 'themes.php', 'customize.php' );
    remove_submenu_page( 'themes.php', 'themes.php' );
    //aremove_menu_page('themes.php');

//    remove_menu_page('ot-settings');
//    remove_menu_page('edit.php?post_type=acf');
//    remove_menu_page('cpt_main_menu');
}

function example_remove_dashboard_widgets()
{
    global $wp_meta_boxes;

    //unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
    //unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    //unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}

add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets');

//add_filter('acf/fields/relationship/query', 'ringier_relationship_options_filter', 10, 3);
//function ringier_relationship_options_filter($options, $field, $the_post) {
//    $options['post_status'] = array('draft', 'pending', 'inherit', 'future', 'publish');
//    return $options;
//}

add_filter('wpseo_stopwords', '__return_empty_array');

add_action('restrict_manage_posts', 'elle_filter_list');

function elle_filter_list()
{
    $screen = get_current_screen();
    global $wp_query;

    switch ($screen->post_type) {
        case 'seafood':
            wp_dropdown_categories(array(
                'show_option_all' => 'Tất cả loại hải sản',
                'taxonomy' => 'seafoodtype',
                'name' => 'seafoodtype',
                'orderby' => 'name',
                'selected' => (isset($wp_query->query['seafoodtype']) ? $wp_query->query['seafoodtype'] : ''),
                'hierarchical' => true,
                'depth' => 3,
                'show_count' => true,
                'hide_empty' => false,
            ));
            break;
    }
}

add_filter('parse_query', 'perform_filtering');

function perform_filtering($query)
{
    global $pagenow;

    $qv = & $query->query_vars;

    //var_dump($qv, $album_id);
    if (is_admin() && $pagenow == 'edit.php') {
        switch ($qv['post_type']) {
            case 'seafood':
                if (($qv['seafoodtype']) && is_numeric($qv['seafoodtype'])) {
                    $term = get_term_by('id', $qv['seafoodtype'], 'seafoodtype');
                    $qv['seafoodtype'] = $term->slug;
                }
                break;
        }
    }
}

function ringier_awesome_image_resize_dimensions($payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop)
{

    // Change this to a conditional that decides whether you
    // want to override the defaults for this image or not.
    if (false)
        return $payload;

    if ($crop) {
        // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
        $aspect_ratio = $orig_w / $orig_h;
        $new_w = min($dest_w, $orig_w);
        $new_h = min($dest_h, $orig_h);

        if (!$new_w) {
            $new_w = intval($new_h * $aspect_ratio);
        }

        if (!$new_h) {
            $new_h = intval($new_w / $aspect_ratio);
        }

        $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);

        $crop_w = round($new_w / $size_ratio);
        $crop_h = round($new_h / $size_ratio);

        //$s_x = 0; // [[ formerly ]] ==> floor( ($orig_w - $crop_w) / 2 );
        $s_x = floor(($orig_w - $crop_w) / 2);
        $s_y = 0; // [[ formerly ]] ==> floor( ($orig_h - $crop_h) / 2 );
    } else {
        // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
        $crop_w = $orig_w;
        $crop_h = $orig_h;

        $s_x = 0;
        $s_y = 0;

        list($new_w, $new_h) = wp_constrain_dimensions($orig_w, $orig_h, $dest_w, $dest_h);
    }

    // if the resulting image would be the same size or larger we don't want to resize it
    if ($new_w >= $orig_w && $new_h >= $orig_h)
        return false;

    // the return array matches the parameters to imagecopyresampled()
    // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
    return array(0, 0, (int)$s_x, (int)$s_y, (int)$new_w, (int)$new_h, (int)$crop_w, (int)$crop_h);
}

add_filter('image_resize_dimensions', 'ringier_awesome_image_resize_dimensions', 10, 6);

add_filter('post_link', 'remove_parent_cats_from_link', 10, 3);

function remove_parent_cats_from_link($permalink, $post, $leavename)
{
    $cats = get_the_category($post->ID);
    if ($cats) {
// Make sure we use the same start cat as the permalink generator
        usort($cats, '_usort_terms_by_ID'); // order by ID
        $category = $cats[0]->slug;
        if ($parent = $cats[0]->parent) {
// If there are parent categories, collect them and replace them in the link
            $parentcats = get_category_parents($parent, false, '/', true);
// str_replace() is not the best solution if you can have duplicates:
// myexamplesite.com/luxemburg/luxemburg/ will be stripped down to myexamplesite.com/
// But if you don't expect that, it should work
            //var_dump($parentcats, $permalink);
            //$permalink = str_replace( $parentcats, '', $permalink );

            $pos = strpos($permalink, $parentcats);
            if ($pos !== false) {
                $permalink = substr_replace($permalink, '', $pos, strlen($parentcats));
            }
        }
    }
    return $permalink;
}
